//Examples/jerasure_02.cpp

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "jerasure.h"
#include "galois.h"

#define talloc(type, num) (type *) malloc(sizeof(type)*(num))

void usage(char *s)
{
	fprintf(stderr, "usage: jerasure_02 r c w - Converts the matrix of jerasure_01 to a bit matrix.\n");
	fprintf(stderr, "       \n");
	fprintf(stderr, "This demonstrates galois_single_multiply(), JER_Matrix::Set(), JER_Matrix::Matrix_To_Bitmatrix(), and JER_Matrix::Print().\n");
	if (s != NULL) fprintf(stderr, "%s\n", s);
	exit(1);
}

int main(int argc, char **argv)
{
	int r, c, w, i, n, j;
	JER_Matrix *jm;
	JER_Matrix *jbm;

	if (argc != 4) usage(NULL);
	if (sscanf(argv[1], "%d", &r) == 0 || r <= 0) usage((char *)"Bad r");
	if (sscanf(argv[2], "%d", &c) == 0 || c <= 0) usage((char *)"Bad c");
	if (sscanf(argv[3], "%d", &w) == 0 || w <= 0) usage((char *)"Bad w");

	jm = new JER_Matrix(r, c, w);

	n = 1;
	for (i = 0; i < r; i++) {
		for (j = 0; j < c; j++) {
			jm->Set(i,j,n);
			n = galois_single_multiply(n, 2, w);
		}
	}

	jbm = jm->Matrix_To_Bitmatrix();
	jbm->Print(w);

	delete jm;
	delete jbm;

	return 0;
}
